<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item prop="name">
        <el-input v-model="queryParams.fileName" placeholder="文件名" clearable size="small" style="width: 200px" />
      </el-form-item>
      <el-form-item prop="name">
        <div class="project-select" @click="selectProject">
          <span>
            <span style="color: #606266;" v-if="projectName">{{projectName}}</span>
            <span style="color: #C0C4CC;" v-else>项目名称</span>
          </span>
          <span v-if="projectName" class="icon" style="color: #C0C4CC;" @click.stop="clearProject"><i class="el-icon-circle-close"></i></span>
        </div>
      </el-form-item>
      <el-form-item prop="testItem">
        <el-date-picker
          v-model="queryParams.month"
          style="width: 200px"
          clearable
          size="small"
          type="month"
          format="yyyy-MM"
          value-format="yyyyMM"
          placeholder="选择月"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- <div class="audit-btn">
      <el-button type="primary" size="mini" @click="upload">上传</el-button>
    </div> -->
    <el-table v-loading="loading" :data="auditList">
      <el-table-column label="文件名" align="center" width="200" prop="fileName" />
      <el-table-column label="月份" align="center" prop="month" />
      <el-table-column label="关联项目" align="center" prop="projectName" width="200" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="更新人" align="center" prop="updateRealName" />
      <el-table-column label="状态" align="center">
        <template v-slot="scope">
          <div>{{ scope.row.isDelete==0?'正常':'已删除' }}</div>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" width="150" prop="createTime" />
      <el-table-column label="更新时间" align="center" width="150" prop="updateTime" />
      <el-table-column label="操作" align="center" width="200">
        <template v-slot="scope">
          <el-button size="mini" type="text" @click="downLoadFile(scope.row)">下载</el-button>
          <el-button v-if="scope.row.isDelete==0" size="mini" type="text" @click="deleteAudit(scope.row.projectReportFileId)">删除</el-button>
          <el-button size="mini" type="text" @click="checkDetail(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="pagination.pageNum" :limit.sync="pagination.pageSize" @pagination="getList" />
    <AuditDetail ref="auditDetailRef" />
    <ReportUpload ref="reportUploadRef" @update="getList" />
    <ProjectConfigModal ref="projectConfigRef" @update="changeProject" />
  </div>
</template>
<script>
import { Base64 } from 'js-base64'
import crypto from 'crypto-js'
import { ossOssPrivateSign } from '@/api/singleraView/teskReport'
import { getTaskReport, updateTaskReport, commonOssFileUrl, getOssConfig } from '@/api/singleraView/teskReport/taskReport'
import AuditDetail from '@/views/singleraView/auditReport/auditDetail.vue'
import ReportUpload from '@/views/singleraView/auditReport/reportUpload.vue'
import ProjectConfigModal from '@/views/auditManagement/projectConfiguration/projectConfigModal.vue'
export default {
  data () {
    return {
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 参数表格数据
      auditList: [],
      pagination: {
        pageNum: 1,
        pageSize: 10,
      },
      projectName: '',
      // 查询参数
      queryParams: {
        month: '',
        projectId: '',
        reportType: 'check',
        isDelete: '',
        fileName: '',
      },
      options: [{
          value: '',
          label: '全部'
        },{
          value: '0',
          label: '正常'
        }, {
          value: '1',
          label: '已删除'
        }],
    };
  },
  components: {
    ProjectConfigModal,
    ReportUpload,
    AuditDetail
  },
  created () {
    this.getList();
  },
  methods: {
    getList () {
      let params = {
        ...this.pagination,
        ...this.queryParams
      }
      getTaskReport(params).then(res => {
        this.auditList = res.rows
        this.total = res.total
      })
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.pagination.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.queryParams = {
        month: '',
        projectId: '',
        reportType: 'check',
        isDelete: '',
        createBy: '',
      }
      this.projectName = ''
      this.pagination = {
        pageNum: 1,
        pageSize: 10,
      }
      this.handleQuery()
    },
    checkDetail(row){
      this.$refs.auditDetailRef.init(row)
    },
    updateAudit(){
      this.handleQuery()
    },
    async downLoadFile(row){
      // const ossConfig = await this.getOSSConfig()
      // const fileUrl = await commonOssFileUrl({
      //   objectFileName: row.filePath,
      //   ...ossConfig
      // });
      // if(fileUrl){
      //   window.open(fileUrl)
      // }

      const rsp = await this.getOSSConfig();
      let params = {
        expiration: rsp.expiration,
        regionId: rsp.regionId,
        requestId: rsp.requestId,
        accessKeyId: rsp.OSSAccessKeyId,
        accessKeySecret: rsp.accessKeySecret,
        bucket: rsp.bucket,
        endPoint: rsp.endPoint,
        securityToken: rsp.securityToken,
      }
      console.log(params)
      const fileUrl = await commonOssFileUrl({
        objectFileName: row.filePath,
        ...params
      });
      if(fileUrl){
       window.open(fileUrl)
      }
    },
    upload(){
      this.$refs.reportUploadRef.init()
    },
    selectProject(){
      this.$refs.projectConfigRef.getInit()
    },
    clearProject(){
      this.queryParams.projectId = ''
      this.projectName = ''
    },
    changeProject(item){
      this.queryParams.projectId = item.value
      this.projectName = item.name
    },
    deleteAudit(id) {
      this.$confirm('是否确认删除稽核标准?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return updateTaskReport(id);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    async getOSSConfig() {
      const rsp = await getOssConfig();
      const policyText = {
        expiration: rsp.expiration, // 设置policy过期时间。
        conditions: [
          // 限制上传大小。
          ["content-length-range", 0, 1024 * 1024 * 1024],
        ],
      };
      const policy = Base64.encode(JSON.stringify(policyText))
      const sha1 = crypto.HmacSHA1(policy, rsp.accessKeySecret);
      const signature = crypto.enc.Base64.stringify(sha1);
      return {
        policy,
        signature,
        expiration: rsp.expiration,
        regionId: rsp.regionId,
        requestId: rsp.requestId,
        OSSAccessKeyId: rsp.accessKeyId,
        accessKeySecret: rsp.accessKeySecret,
        bucket: rsp.bucket,
        endPoint: rsp.endPoint,
        securityToken: rsp.securityToken,
        'x-oss-security-token': rsp.securityToken
      }
    },
  }
}
</script>
<style scoped lang="scss">
.audit-btn{
  margin-bottom: 20px;
  display: flex;
  flex-direction: row;
  .upload-demo{
    margin-right: 10px;
  }
}
.project-select{
  width: 200px;
  height: 34px;
  line-height: 34px;
  background: #FFFFFF;
  border-radius: 5px;
  color: #C0C4CC;
  padding: 0 20px 0 15px;
  cursor: pointer;
  border: 1px solid #DCDFE6;
  position: relative;
  overflow: hidden;
  .icon{
    position: absolute;
    right: 5px;
    top: 0;
  }
}
</style>

